package com.lai.shop.service.impl;



import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lai.shop.utils.PageUtils;
import com.lai.shop.utils.Query;

import com.lai.shop.dao.CategoryDao;
import com.lai.shop.entity.CategoryEntity;
import com.lai.shop.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 1、查出所有分类
     * 2、组装成父子的树形结构
     */
    @Override
    public List<CategoryEntity> listTree() {
        //1、找出所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);

        List<CategoryEntity> collect = categoryEntities.stream().filter(categoryEntity -> {
            //2、找出所有一级分类
            return categoryEntity.getParentId() == 0L;
        }).map(categoryEntity -> {
            //3、找出该一级分类的所有子分类
            List<CategoryEntity> childrens = getChildrens(categoryEntity, categoryEntities);
            categoryEntity.setChildren(childrens);
            return categoryEntity;
        }).collect(Collectors.toList());

        return collect;
    }


    /**
     * 获取父菜单的子菜单  递归查找所有菜单的子菜单
     * @param root 父菜单
     * @param categoryEntities 全部菜单
     * @return
     */
    private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> categoryEntities) {

        List<CategoryEntity> collect = categoryEntities.stream().filter(categoryEntity -> {
            //1、找出该父分类的所有的 下一级的子分类
            return categoryEntity.getParentId().equals(root.getId());
        }).map(categoryEntity -> {
            //2、递归找出当前父菜单的所有子菜单  为每一个categoryEntity设置子菜单
            List<CategoryEntity> childrens = getChildrens(categoryEntity, categoryEntities);
            categoryEntity.setChildren(childrens);
            return categoryEntity;
        }).collect(Collectors.toList());

        return collect;
    }



    @Override
    public Long[] findCategoryPath(Long id) {
        ArrayList<Long> paths = new ArrayList<>();
        //找出该分类的全部父级分类
        List<Long> parentPath = findCategoryParentPath(id, paths);
        //反转
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }


    /**
     * 以递归的方式找出 catelogId 的完整路径 例如[2, 34, 225]
     *
     * @param id 当前节点id
     * @return 完整路径
     * @paths 完整路径的list
     */
    private List<Long> findCategoryParentPath(Long id, List<Long> paths) {
        //1 进入  首先收集当前节点的id
        paths.add(id);
        //2、根据当前节点的id找出   详细信息
        CategoryEntity category = this.getById(id);
        //3、如果 详细信息 中有父id(不是一级分类)
        if (category.getParentId() != 0) {
            //4、递归 继续找
            findCategoryParentPath(category.getParentId(), paths);
        }
        return paths;
    }






}
